*** Set $root and preliminaries
cap log close
log using "output/logs/In text values.log", replace 

********************************************************************************
*** Abstract / ITT percent effects
use "data/pseudodata/userlevel_pseudodata.dta", clear
*use "data/cleaned/collapsed action with credit.dta", clear

replace anybound7 = anybound7*100
qui reg anybound7 daily, r
di "ITT take-up of any coverage  = " round(_b[daily],.1)
di "Percent increase in take-up of any coverage = " round(_b[daily]/_b[_cons]*100,1)

qui reg daysnonzeroreserve90cr daily, r
di "ITT days with any coverage  = " round(_b[daily],.1)
di "Percent increase in days with any coverage = " round(_b[daily]/_b[_cons]*100,1)

replace bound = bound*100
qui reg bound daily, r
di "ITT take-up of experiment coverage  = " round(_b[daily],.1)
di "Percent increase in take-up of coverage through expt = " round(_b[daily]/_b[_cons]*100,1)



/********************************************************************************
*** Abstract / INF Numbers

Note: I did not generate psuedodata for the Stripe timeline but the calculation is available here:

* Percent of enrolled drivers have a purchase rejected for insufficient funds
use "data/cleaned/stripe/hugo stripe timeline.dta", clear
bysort stripecustomerid: egen isbound=max(bound)
bysort stripecustomerid: egen isdaily=max(daily)
qui levelsof stripecustomerid if isbound==1 & isdaily==1
local all=`r(r)'
assert `all' == 231
qui levelsof stripecustomerid if isbound==1 & isdaily==1 & ///
	stripe_message=="The bank returned the decline code `insufficient_funds`."
di round(`r(r)'/`all'*100,.1) " percent of enrolled drivers have a purchase rejected for insufficient funds"
qui levelsof stripecustomerid if isbound==1 & isdaily==1 & card_type=="debit" & ///
	stripe_message=="The bank returned the decline code `insufficient_funds`."
di round(`r(r)'/`all'*100,.1) " percent of enrolled drivers have a DEBIT purchase rejected for insufficient funds"
qui levelsof stripecustomerid if isbound==1 & isdaily==1 & card_type=="prepaid" & ///
	stripe_message=="The bank returned the decline code `insufficient_funds`."
di round(`r(r)'/`all'*100,.1) " percent of enrolled drivers have a PREPAID purchase rejected for insufficient funds"

* Percent with INF as last observable action before attriting from coverage
* keep only stripe transactions
keep if !mi(status)
* sort to use order of transaction 
sort stripecustomerid timestamp
replace status = "INF" if ///
	stripe_message == "The bank returned the decline code `insufficient_funds`."
* flag INFs that are last payment action
gen terminalinf = 1 if status == "INF" & stripecustomerid != stripecustomerid[_n+1]
* flag INFs that are followed by another INF
gen infstreak = 1 if status == "INF" & status[_n+1] == "INF"
gen paid = 1 if status == "Paid"
* save timestamp only if transaction successful
gen nextpaydate = paid*timestamp
replace nextpaydate = nextpaydate[_n+1] if mi(nextpaydate) & ///
	stripecustomerid == stripecustomerid[_n+1]

* Actions after an INF (at the INF-level)
gen inftype = ""
replace inftype = "Followed by Another INF" if infstreak == 1
replace inftype = "INF Last Action" if terminalinf == 1
replace inftype = "Followed by Payment" if infstreak != 1 & terminalinf != 1 & status == "INF"
tab inftype 

* Percent of enrolled drivers who attrit after an INF
qui levelsof stripecustomerid if isbound==1 & inftype=="INF Last Action" & isdaily==1
di round(`r(r)'/`all'*100,1) " percent of enrolled drivers have INF as last payment action"

* define time to next payment (to estimate after INF)
gen timetonextpay = (nextpaydate - timestamp)/(1000*60*60*24)
* Get average of time to next payment
sum timetonextpay if status == "INF"
di round(`r(mean)',1) " days to successful payment after INF on average"

* Count number who attrit after INF
qui count if inftype=="INF Last Action"
local inflast = `r(N)'

use "data/cleaned/collapsed action with credit.dta", clear
qui count if threemonther==0 & daily==1 & bound==1
di round(`inflast'/`r(N)'*100,.1) " percent of all pay-as-you-go attrition have INF as last action"
*/ 

********************************************************************************
*** Intro / Insurance Utilization Rate / Deactivation Rate

* Average share of days with insurance turned on / off
use "data/pseudodata/userlevel_pseudodata.dta", clear
*use "data/cleaned/collapsed action with credit.dta", clear

* Define utilization rate to get loan duration (defined at the person-level then averaged)
gen shareins = totdayscoverage90/daysnonzeroreserve90 if daily==1 & bound==1
summ shareins if daily==1, det
di round(`r(mean)'*100,.1) " share days with coverage available insured"

gen shareoff = 1-totdayscoverage90/daysnonzeroreserve90 if daily==1 & bound==1
qui summ shareoff
di round(`r(mean)'*100,.1) " share days with coverage available and insurance turned off"

********************************************************************************
*** Intro / Stats on forgoing bundle
use "data/pseudodata/userlevel_pseudodata.dta", clear
*use "data/cleaned/collapsed action with credit.dta", clear

*Without the discount, 28 percent of days purchased are in bundles of 14 or 30 days. 
*fracbund
*(num is saved as non-first payments)
replace num3 = num3 + 1 if paydays1 == 3
replace num7 = num7 + 1 if paydays1 == 7
replace num14 = num14 + 1 if paydays1 == 14 | paydays1 == 12
replace num30 = num30 + 1 if paydays1 == 30 | paydays1 == 24

gen daybund = 14*num14+30*num30
gen fracdbund = daybund/(3*num3+7*num7+14*num14+30*num30)

qui reg fracdbund bundle, r
di _b[_cons]
di round(_b[_cons]*100,1) " percent of purchases are bundles when discount is not offered"
di _b[bundle]
di round(_b[bundle]*100,1) "pp increase in percent of days purchased in bundles when discount is offered"

/* Note: Did not generate these variables in pseudodata
* Percent of drivers forgo discounts for all of their purchases
sum neverbundler if bundle==1
di round(`r(mean)'*100,1) " percent of drivers offered discount who forgo it on every purchase"
* Percent of drivers forgo discounts for at least one of their purchases
qui summ evernonbundler if bundle==1
di round(`r(mean)'*100,1) " percent of drivers offered discount who forgo it at least once"

********************************************************************************
*** Share merged to credit reports and constrained by definition
use "data/pseudodata/userlevel_pseudodata.dta", clear
*use "data/cleaned/collapsed action with credit.dta", clear

* 85% match to a credit report
tab gotcred, mi

* Credit constrained = 80.8% when counting those missing credit report and 77.5% with credit report
sum isconstrained
di round(`r(mean)'*100,.1) " percent of applicants are credit constrained"
sum isconstrained if gotcred=="Has Report"
di round(`r(mean)'*100,.1) " percent of applicants have no available credit on their report"
*/
********************************************************************************
*** Section 3.1, Utilization rate and days with coverage available and active
use "data/pseudodata/userlevel_pseudodata.dta", clear
*use "data/cleaned/collapsed action with credit.dta", clear

reg totdayscoverage90 daily if bound==1, r
reg totdayscoverage90cr daily if bound==1, r
sum daysnonzeroreserve90 if daily==0 & bound==1, det
sum totdayscoverage90 if daily==0 & bound==1, det

sum totdayscoverage90 if daily==1 & bound==1, det

* Define utilization rate to get loan duration (defined at the person-level then averaged)
gen shareins = totdayscoverage90/daysnonzeroreserve90 if daily==1 & bound==1
summ shareins if daily==1, det
di round(`r(mean)'*100,.1) " share days with coverage available insured"

* "Pay-as-you-go drivers insure 37.3 days on average..."
sum totdayscoverage90 if daily==1 & bound==1
local insured_days = r(mean)
di round(`r(mean)',.1) " number of days insured by pay-as-you-go enrollees on average"

* "...versus 78.0 days for drivers who enroll in the traditional contract."
sum totdayscoverage90 if daily==0 & bound==1, det
di round(`r(mean)',.1) " number of days insured by control enrollees on average"

* "Drivers enrolled in pay-as-you-coverage had 62.5 days with coverage available on average..."
sum daysnonzeroreserve90 if daily==1 & bound==1, det
local available_days = r(mean)
di round(`r(mean)',.1) " number of days with coverage available for pay-as-you-go enrollees"

* "... insurance was active on 59.6 percent of those days."
local share_active = `insured_days' / `available_days'
di round(`share_active'*100,.1) " insurance active on 59.6 percent of those days"



********************************************************************************
*** Section 3.2 Treatment Arms / K-S tests of equality of distribution by treatment arm
use "data/pseudodata/userlevel_pseudodata.dta", clear
*use "data/cleaned/collapsed action with credit.dta", clear

ksmirnov totalagentpremium, by(daily) exact
ksmirnov totalagentpremium if inlist(price, "base", "high"), by(price) 
ksmirnov totalagentpremium if inlist(price, "base", "low"), by(price)
ksmirnov totalagentpremium if inlist(price, "low", "high"), by(price) 
ksmirnov totalagentpremium if daily==1, by(bundle)

********************************************************************************
*** Sec 4.2, Demand Elasticity

use "data/pseudodata/userlevel_pseudodata.dta", clear
*use "data/cleaned/collapsed action with credit.dta", clear

sum bound if daily==1
local mean_bound = r(mean)
sum totdayscoverage90 if daily==1
local mean_days = r(mean)

reg bound dayrate totalagentpremium
di round(_b[dayrate]/`mean_bound'*100,1) " percent off of a mean take-up rate of " round(100*`mean_bound',.1)
reg totdayscoverage90 dayrate totalagentpremium
di round(_b[dayrate]/`mean_days'*100,1) " percent off of a mean days insured of " round(`mean_days',.1)

********************************************************************************
*** Section 4.3, Payments more likely on Fridays 
/* Note: I did not generate pseudodata for the balance actions dataset
use "data/cleaned/hugo/All balance actions.dta"  ///
	if inlist(source,"manual_sms","manual_web","initial_bind")==1, clear

*just two for all purchases and 3/7 day purchases
gen weekday = dow(dofc(timestamp))

* Share of purchases 
 assert type=="add_balance"

count if weekday!=5 // not Fridays
local num_notfridays = `r(N)'/6
count if weekday==5 // Fridays
local num_fridays = `r(N)'

di "Purchases are " round(100*(`num_fridays'-`num_notfridays')/`num_notfridays',1) " percent more likely on Fridays"
*/

log close 
